<template>
  <div>

<h3>home</h3>

  </div>
</template>
<script>
import { crc8, crc16, crc32 } from 'easy-crc';
export default {
  data() {

    
    let options = [{

      label: 'CRC8',
      children: [{
        label: 'CRC-8',
      }, {
        label: 'CDMA2000',
      }, {
        label: 'DARC',
      }, {
        label: 'DVB-S2',
      }, {
        label: 'EBU',
      }, {
        label: 'I-CODE',
      }, {
        label: 'ITU',
      }, {
        label: 'MAXIM',
      }, {
        label: 'ROHC',
      }, {
        label: 'WCDMA',
      }]
    }, {
      label: 'CRC16',
      children: [{
        label: 'BUYPASS',
      }, {
        label: 'CCITT-FALSE',
      }, {
        label: 'ARC',
      }, {
        label: 'CDMA2000',
      }, {
        label: 'DDS-110',
      }, {
        label: 'DECT-R',
      }, {
        label: 'DECT-X',
      }, {
        label: 'DNP',
      }, {
        label: 'EN-13757',
      }, {
        label: 'GENIBUS',
      }, {
        label: 'MAXIM',
      }, {
        label: 'KERMIT',
      }, {
        label: 'MCRF4XX',
      }, {
        label: 'MODBUS',
      }, {
        label: 'RIELLO',
      }, {
        label: 'T10-DIF',
      }, {
        label: 'TELEDISK',
      }, {
        label: 'TMS37157',
      }, {
        label: 'USB',
      }, {
        label: 'X-25',
      }, {
        label: 'XMODEM',
      }, {
        label: 'CRC-A',
      }]
    }, {

      label: 'CRC32',
      children: [{
        label: 'CRC-32',
      }, {
        label: 'CRC-32C',
      }, {
        label: 'CRC-32D',
      }, {
        label: 'CRC-32Q',
      }, {
        label: 'BZIP2',
      }, {
        label: 'JAMCRC',
      }, {
        label: 'MPEG-2',
      }, {
        label: 'POSIX',
      }, {
        label: 'XFER',
      }]
    }]

    options.map(v => {
      v.value = v.label
      v.children.map(n => {
        n.value = n.label
      })
    })
    return {
      hex: '01 03 02 03 B6',
      value: ['CRC16', 'MODBUS'],
      options,
      hexRes: null,
      type: true

    }
  },
  async  mounted() {
    // console.log(3)
    const SerialPort = require('serialport')
    const InterByteTimeoutParser = require('@serialport/parser-inter-byte-timeout')

    console.log(SerialPort,InterByteTimeoutParser)
    let list = await SerialPort.SerialPort.list()
    console.log('SerialPort',list)

    console.log(process.env.NODE_ENV)
    let data = Buffer.from('340304FFFFFFA7', 'hex');
    let checksum = crc16('MODBUS', data);
    console.log(checksum, 'checknum')
    let hex = checksum.toString(16)
    console.log(typeof checksum, checksum.toString(16));
    console.log(hex.substring(0, 2))
    console.log(hex.substring(2, 4))


    console.log(this.translate('3b47')) //2018915346)


  },
  methods: {

    translate(num) {
      return num.match(/\w{2}/g).reverse().join('')
    },
    convert() {
      if (!this.hex || !this.value) {
        return
      }
      let data = Buffer.from(this.hex.replace(/ /g, ''), 'hex');

      console.log(this.value[0], this.value[1])

      let res
      if (this.value[0] == 'CRC8') {
        res = crc8(this.value[1], data)
      } else if (this.value[0] == 'CRC16') {
        res = crc16(this.value[1], data)
      } else if (this.value[0] == 'CRC32') {
        res = crc32(this.value[1], data)
      }
      let strhex = res.toString(16)
      console.log(strhex)
      if (strhex.length % 2 != 0) {
        strhex = '0' + strhex
      }
      console.log(strhex)

      if (this.type) {
        this.hexRes = strhex
      } else {
        this.hexRes = this.translate(strhex)
      }



    },
    handleChange(value) {
      console.log(value);


    }
  }
}
</script>